var app = getApp();
Page({
  data: {
    curTab: 0,
    noData: true,
    order_data: [],
    data_url: "",
    winHeight: 0,
  },
  /**
   * 确认收货
   * @method
   */
  agreegoods: function (e) {
    var ordernumber = e.currentTarget.dataset.a
    var that = this
    wx.request({
      url: app.globalData.data_url + '/GPD/harvestGoods.do',
      data: {
        session_id: app.globalData.sessionId,
        ordernumber: ordernumber
      },
      success: function (res) {
        if (res.data.code == 0) {
          wx.showToast({
            title: '操作成功',
          })
          switch (that.data.curTab) {
            case '0':
              that.pullOrder0()
              break;
            case '1':
              that.pullOrder1()
              break;
            case '2':
              that.pullOrder2()
              break;
            case '3':
              that.pullOrder3()
              break;
            case '4':
              that.pullOrder4()
              break;
          }
        } else {
          wx.showToast({
            title: res.data.message,
            image: '/images/close.png'
          })
        }
      }
    })
  },
  // 删除订单
  clearOrder: function (res) {
    var that = this
    var ordernumber = res.target.dataset.a
    wx.showModal({
      title: '温馨提示',
      content: '是否要放弃此订单？',
      success: function (res) {
        if (res.confirm) {
          app.getSessionId().then(function () {
            wx.request({
              url: that.data.data_url + "/GPD/cancelOrder.do",
              data: {
                session_id: app.globalData.sessionId,
                ordernumber: ordernumber
              },
              success: function (res) {
                if (res.data.code == 0) {
                  wx.showToast({
                    title: '取消成功',
                  })
                  switch (that.data.curTab) {
                    case '0':
                      that.pullOrder0()
                      break;
                    case '1':
                      that.pullOrder1()
                      break;
                    case '2':
                      that.pullOrder2()
                      break;
                    case '3':
                      that.pullOrder3()
                      break;
                    case '4':
                      that.pullOrder4()
                      break;
                  }
                } else {
                  wx.showToast({
                    title: res.data.message,
                    image: '/images/close.png'
                  })
                }
              }
            })
          })
        }
      }
    })
  },
  // 支付
  nowPay: function (res) {
    var that = this
    var ordernumber = res.target.dataset.a
    app.getSessionId().then(function () {
      wx.request({
        url: that.data.data_url + "/GPD/pay.do",
        data: {
          session_id: app.globalData.sessionId,
          ordernumber: ordernumber
        },
        success: function (res) {
          var data = {
            timeStamp: res.data.timeStamp,
            nonceStr: res.data.nonceStr,
            package: res.data.package,
            signType: res.data.signType,
            paySign: res.data.paySign,
          }
          wx.requestPayment({
            timeStamp: res.data.timeStamp,
            nonceStr: res.data.nonceStr,
            package: res.data.package,
            signType: res.data.signType,
            paySign: res.data.paySign,
            success: function (res) {
              wx.showLoading({
                title: '加载中',
              })
              wx.request({
                url: that.data.data_url + '/GPD/getPayInfo.do',
                data: {
                  ordernumber: that.data.ordernumber
                },
                success(res) {
                  wx.hideLoading()
                  if (res.data.message == "success") {
                    wx.showModal({
                      title: '提示',
                      content: '您已购买成功，希望前往',
                      cancelText: '订单',
                      confirmText: '首页',
                      success: function (res) {
                        if (res.confirm) {
                          wx.switchTab({
                            url: '/pages/index/index',
                          })
                        } else if (res.cancel) {
                          wx.redirectTo({
                            url: '/pages/my/my',
                          })
                        }
                      }
                    })
                    switch (that.data.curTab) {
                      case '0':
                        that.pullOrder0()
                        break;
                      case '1':
                        that.pullOrder1()
                        break;
                      case '2':
                        that.pullOrder2()
                        break;
                      case '3':
                        that.pullOrder3()
                        break;
                      case '4':
                        that.pullOrder4()
                        break;
                    }
                  }
                }
              })
            },
            fail: function (res) {
              wx.showToast({
                title: '支付失败',
                icon: 'loading'
              })
            },
            complete: function (res) {
            }
          })
        }
      })
    })
  },
  /**
   * 退款
   * @request
   */
  refund: function (e) {
    var that = this
    var ordernumber = e.currentTarget.dataset.a
    wx.showModal({
      title: '提示',
      content: '退款需要审核3-5个工作日，是否确认退款？',
      success: function (res) {
        if (res.confirm) {
          wx.request({
            url: that.data.data_url + '/GPD/applyRefund.do',
            data: {
              session_id: app.globalData.sessionId,
              ordernumber: ordernumber
            },
            success: function (res) {
              if (res.data.code == 0) {
                wx.showToast({
                  title: '申请成功',
                })
              } else {
                wx.showToast({
                  title: res.data.message,
                  image: '/images/close.png'
                })
              }
            }
          })
        }
      }
    })
  },
  pullOrder0: function (res) {
    var that = this
    app.getSessionId().then(function () {
      wx.request({
        url: that.data.data_url + "/GPD/getFullOrder.do",
        data: {
          session_id: app.globalData.sessionId
        },
        success: function (res) {
          wx.hideLoading()
          res.data = res.data.map(function (item) {
            item.order.totalPrice = item.order.totalPrice.toFixed(2)
            item.order.payPrice = item.order.payPrice.toFixed(2)
            item.order.paysum = item.order.paysum.toFixed(2)
            return item
          })
          that.setData({
            order_data: res.data
          })
        }
      })
    })
  },
  // 拉取待支付订单
  pullOrder1: function (res) {
    var that = this
    wx.request({
      url: that.data.data_url + "/GPD/getWaitForPaymentOrderByConsumer.do",
      data: {
        session_id: app.globalData.sessionId
      },
      success: function (res) {
        wx.hideLoading()
        res.data = res.data.map(function (item) {
          item.order.totalPrice = item.order.totalPrice.toFixed(2)
          item.order.payPrice = item.order.payPrice.toFixed(2)
          item.order.paysum = item.order.paysum.toFixed(2)
          return item
        })
        that.setData({
          order_data: res.data
        })
      }
    })
  },
  // 拉取待发货订单
  pullOrder2: function (res) {
    var that = this
    wx.request({
      url: that.data.data_url + "/GPD/getUnsend.do",
      data: {
        session_id: app.globalData.sessionId
      },
      success: function (res) {
        wx.hideLoading()
        res.data = res.data.map(function (item) {
          item.order.totalPrice = item.order.totalPrice.toFixed(2)
          item.order.payPrice = item.order.payPrice.toFixed(2)
          item.order.paysum = item.order.paysum.toFixed(2)
          return item
        })
        that.setData({
          order_data: res.data
        })
      }
    })
  },
  // 拉取待收货订单
  pullOrder3: function (res) {
    var that = this
    wx.request({
      url: that.data.data_url + "/GPD/getUngaind.do",
      data: {
        session_id: app.globalData.sessionId
      },
      success: function (res) {
        wx.hideLoading()
        res.data = res.data.map(function (item) {
          item.order.totalPrice = item.order.totalPrice.toFixed(2)
          item.order.payPrice = item.order.payPrice.toFixed(2)
          item.order.paysum = item.order.paysum.toFixed(2)
          return item
        })
        that.setData({
          order_data: res.data
        })
      }
    })
  },
  // 拉取退换货订单
  pullOrder4: function (res) {
    var that = this
    wx.request({
      url: that.data.data_url + "/GPD/getReturnpolicy.do",
      data: {
        session_id: app.globalData.sessionId,
        row: 999
      },
      success: function (res) {
        wx.hideLoading()
        res.data = res.data.map(function (item) {
          item.order.totalPrice = item.order.totalPrice.toFixed(2)
          item.order.payPrice = item.order.payPrice.toFixed(2)
          item.order.paysum = item.order.paysum.toFixed(2)
          return item
        })
        that.setData({
          order_data: res.data
        })
      }
    })
  },
  //点击切换tab
  swichNav: function (e) {
    var that = this;
    var cur = e.target.dataset.current
    if (this.data.curTab === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        curTab: e.target.dataset.current
      })
      wx.showLoading({
        title: '加载中',
      })
      switch (cur) {
        case '0':
          that.pullOrder0()
          break;
        case '1':
          that.pullOrder1()
          break;
        case '2':
          that.pullOrder2()
          break;
        case '3':
          that.pullOrder3()
          break;
        case '4':
          that.pullOrder4()
          break;
      }
    }
  },
  onLoad: function (options) {
    var that = this;
    wx.showLoading({
      title: '加载中',
    })
    this.setData({
      data_url: app.globalData.data_url
    })
    that.setData({
      curTab: options.id
    })
    console.log(options.id)
    switch (options.id) {
      case '0':
        that.pullOrder0()
        break;
      case '1':
        that.pullOrder1()
        break;
      case '2':
        that.pullOrder2()
        break;
      case '3':
        that.pullOrder3()
        break;
      case '4':
        that.pullOrder4()
        break;
    }
  },
  onReady: function () {
    // wx.request({
    //   url: that.data.data_url + + "/GPD/getWaitForPaymentOrderByConsumer.do",
    // })
  },
  onShow: function () {

  },
  onHide: function () {

  },
  onUnload: function () {

  },
  onPullDownRefresh: function () {

  },
  onReachBottom: function () {

  }
})    